import { createSlice } from '@reduxjs/toolkit'
import { initialState } from './data'
import { setNoteListData } from './action'
import { get_note_list_data_async } from './request'

export const noteSlice = createSlice({
  name: 'note',
  initialState,
  // 这些 reducer 函数用于处理与当前切片（slice）相关的同步 action
  reducers: {
    set_note_list_data: setNoteListData
  },
  // extraReducers 用于处理异步 thunk 的不同状态（pending、fulfilled、rejected）。
  extraReducers: builder => {
    builder
      // 获取笔记列表
      .addCase(get_note_list_data_async.pending, (state, res) => {
        state.loading = true
      })
      .addCase(get_note_list_data_async.fulfilled, (state, res) => {
        state.loading = false
        state.note_list_data.count = res.payload.data.count
        state.note_list_data.list = res.payload.data.data
      })
  }
})

export const { set_note_list_data } = noteSlice.actions

export default noteSlice.reducer
